package com.bocai.yuweiwangluo.activity;


import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.View.XListView;
import com.bocai.yuweiwangluo.base.BaseActivity;
import com.bocai.yuweiwangluo.bean.CityAndTypeBean;
import com.bocai.yuweiwangluo.bean.PopBean;
import com.bocai.yuweiwangluo.bean.VenueListBean;
import com.bocai.yuweiwangluo.presenter.VenueListPresenter;
import com.bocai.yuweiwangluo.utils.PopUtil;
import com.bocai.yuweiwangluo.viewModel.IVenueListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

/**
 * 场馆列表界面
 */
public class VenueActivity extends BaseActivity implements View.OnClickListener,AdapterView.OnItemClickListener,IVenueListView,XListView.IXListViewListener
{
    @ViewInject(R.id.ll_nocotent)
    LinearLayout llnoContent;
    @ViewInject(R.id.lv_venue_list)
    XListView lv_venue_list;
    @ViewInject(R.id.ll_venue_sporttype)
    LinearLayout ll_venue_sporttype;
    @ViewInject(R.id.ll_venue_area)
    LinearLayout ll_venue_area;
    @ViewInject(R.id.ll_venue_sort)
    LinearLayout ll_venue_sort;
    @ViewInject(R.id.tv_venue_sporttype)
    TextView tv_venue_sporttype;
    @ViewInject(R.id.tv_venue_area)
    TextView tv_venue_area;
    @ViewInject(R.id.tv_venue_sort)
    TextView tv_venue_sort;
    @ViewInject(R.id.iv_venue_sporttype)
    ImageView iv_venue_sporttype;
    @ViewInject(R.id.iv_venue_area)
    ImageView iv_venue_area;
    @ViewInject(R.id.iv_venue_sort)
    ImageView iv_venue_sort;
    @ViewInject(R.id.ll_refresh)
    LinearLayout llRefresh;
    private int width;
    private boolean popisshow=false;
    private PopUtil pu;
    private ArrayList<PopBean> list;
    private VenueListPresenter venueListPresenter;
    private VenueListBean venueListBean;
    private CityAndTypeBean cityAndTypeBean;
    private String type_id="0";
    private String city_id="0";
    private int page=1;
    private String sortType="";
    @Override
    protected int contentView()
    {
        return R.layout.activity_venue;
    }

    @Override
    protected void setData()
    {
        ViewUtils.inject(this);
        initToolbar("场地");
        getVenueListPresenter();
        WindowManager windowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        width=windowManager.getDefaultDisplay().getWidth() / 3;
        venueListPresenter.getVenueList("1", "0", "0");
        initEvent();
    }

    @Override
    protected void initEvent()
    {
        super.initEvent();
        llRefresh.setOnClickListener(this);
        lv_venue_list.setOnItemClickListener(this);
        ll_venue_sporttype.setOnClickListener(this);
        ll_venue_area.setOnClickListener(this);
        ll_venue_sort.setOnClickListener(this);
        lv_venue_list.setPullLoadEnable(true);
        lv_venue_list.setPullRefreshEnable(true);
        lv_venue_list.setXListViewListener(this);

    }


    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ll_refresh:
                llRefresh.setVisibility(View.GONE);
                onRefresh();
                break;
            case R.id.ll_venue_sporttype:
                cityAndTypeBean=venueListPresenter.getCityAndTypeBean();
                if(cityAndTypeBean.getCity()!=null) {
                    showPopwindow(v, tv_venue_sporttype, setTypeList(), 0,width,width*2);
                }
                break;
            case R.id.ll_venue_area:
                cityAndTypeBean=venueListPresenter.getCityAndTypeBean();
                if(cityAndTypeBean.getType()!=null) {
                    showPopwindow(v, tv_venue_area, setCityList(), 1,width,width*2);
                }
                break;
            case R.id.ll_venue_sort:
                int height=ll_venue_sort.getHeight();
                    showPopwindow(v, tv_venue_sort, setSortList(), 2, width, height*2);
                break;
        }
    }
    //设置地区数据
    private ArrayList<PopBean> setCityList() {
        try
        {
            list=new ArrayList<>();
            cityAndTypeBean=venueListPresenter.getCityAndTypeBean();
            if(cityAndTypeBean!=null){
                for (int i=0;i<cityAndTypeBean.getCity().size()+1;i++)
                {
                    PopBean popBean=new PopBean();
                    if(i==0){
                        popBean.setId("0");
                        popBean.setName("全部地区");
                    }else{
                        popBean.setId(cityAndTypeBean.getCity().get(i-1).getId());
                        popBean.setName(cityAndTypeBean.getCity().get(i - 1).getName());
                    }

                    list.add(i,popBean);
                }
            }
        }catch (Exception e){}

        return list;
    }
    //设置运动类型数据
    private ArrayList<PopBean> setTypeList() {
        try
        {

            list=new ArrayList<>();
            cityAndTypeBean=venueListPresenter.getCityAndTypeBean();
            if(cityAndTypeBean!=null){

                for (int i=0;i<cityAndTypeBean.getType().size()+1;i++)
                {
                    PopBean popBean=new PopBean();
                    if(i==0){
                        popBean.setId("0");
                        popBean.setName("全部运动");
                    }else {
                        popBean.setId(cityAndTypeBean.getType().get(i- 1).getId());
                        popBean.setName(cityAndTypeBean.getType().get(i-1).getTitle());
                    }
                    list.add(i,popBean);
                }
            }
        }catch (Exception e){}
        return list;
    }
    //设置排序方式
    private ArrayList<PopBean> setSortList() {
        try
        {
            list=new ArrayList<>();
            String titles[]={"距离最近","价格最低"};
            String ids[]={"dist","price"};
            for (int i=0;i<titles.length;i++)
            {
                PopBean popBean=new PopBean();
                popBean.setId(ids[i]);
                popBean.setName(titles[i]);
                list.add(i,popBean);
            }
        }catch (Exception e){

        }
        return list;
    }
    private void showPopwindow(View v,final TextView tv,ArrayList<PopBean> list,final int what,int width,int height)
    {
        try{

            pu=PopUtil.getInstance(this,v,width,height,list);
            pu.setListener(new PopUtil.OnDatePopClickListener()
            {
                @Override
                public void onClick(PopUtil pu, PopBean date)
                {
                    try{
                        tv.setText(date.getName());
                        pu.dismiss();
                        if(0==what)//运动类型
                        {
                            type_id=date.getId();
                            page=1;
                            venueListPresenter.getVenueList(page+"", type_id, city_id);
                        }else if(1==what)//地区
                        {
                            city_id=date.getId();
                            page=1;
                            venueListPresenter.getVenueList(page+"",type_id,city_id);
                        }else//排序方式
                        {
                            sortType=date.getId();
                            page=1;
                            venueListPresenter.getVenueList(page+"",type_id,city_id);

                        }
                    }catch (Exception e){}
                }
            });
            pu.show();
        }catch (Exception e){}
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        try
        {
            VenueListBean.Data data= (VenueListBean.Data) parent.getAdapter().getItem(position);
            Intent intent=new Intent(this,VenueBookActivity.class);
            intent.putExtra("id",data.getId());
            intent.putExtra("title",data.getTitle());
            intent.putExtra("content",data.getContent());
            intent.putExtra("local_x",data.getLocal_x());
            intent.putExtra("local_y",data.getLocal_y());
            startActivity(intent);
        }catch (Exception e){}
    }

    @Override
    public String  getSortType() {
        return sortType;
    }

    @Override
    public int getPage() {
        return page;

    }

    @Override
    public void load() {
        lv_venue_list.stopRefresh();
        lv_venue_list.stopLoadMore();
        lv_venue_list.setRefreshTime("");
    }

    @Override
    public XListView getListView() {
        return lv_venue_list;
    }

    @Override
    public LinearLayout getnoContent() {
        return llnoContent;
    }

    @Override
    public VenueListPresenter getVenueListPresenter() {
        venueListPresenter=new VenueListPresenter(this,this,this);
        return venueListPresenter;
    }

    @Override
    public void finishActivity() {
        this.finish();
    }

    @Override
    public TextView getTitleRight() {
        return null;
    }

    @Override
    public LinearLayout getllRefresh() {
        return llRefresh;
    }

    @Override
    public String getVenue() {
        return "";
    }

    @Override
    public void onRefresh() {
        page=1;
        lv_venue_list.setPullLoadEnable(true);
        venueListPresenter.getVenueList(page+"", type_id, city_id);
    }

    @Override
    public void onLoadMore() {
        page++;
        venueListPresenter.getVenueList(page+"", type_id, city_id);
    }
}
